<template>
  <div class="app-container">
    <div style="padding: 5px 0px">
      <toolbar
        :table="$refs.table"
        @add="handleEdit"
        @delete="handleDelete"
        @refresh="handleRefresh"
      >
      </toolbar>
    </div>
    <div>
      <el-table
        :data="list"
        border
        fit
        highlight-current-row
        ref="table"
        @cell-dblclick="hanldeDbClick"
        size="small"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品名称">
                <span>{{ props.row.number }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="日期">
          <template slot-scope="scope">
            {{ scope.row.date }}
          </template>
        </el-table-column>
        <el-table-column label="凭证号">
          <template slot-scope="scope">
            {{ scope.row.fullNumber }}
          </template>
        </el-table-column>
        <el-table-column label="来源">
          <template slot-scope="scope">
            {{ dataSourceTypeName(scope.row.sourceType) }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.approved == false"
              >已审核</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleEdit(scope.row.id)"
              >{{ $t("common.edit") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
        :current-page.sync="filter.pageIndex"
        :page-sizes="[10, 20, 50, 100, 200, 500]"
        :page-size.sync="filter.pageSize"
        :prev-click="handlePrePage"
        :next-click="handleNextPage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import toolbar from "@/components/Bar/toolbar.vue";
import { getJournals, deleteJournal } from "@/api/journal";
export default {
  components: {
    toolbar,
  },
  data() {
    return {
      list: null,
      filter: {
        pageIndex: 1,
        pageSize: 10,
        keyword: "",
        approved: null,
      },
      total: 0,
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      getJournals(this.filter).then((res) => {
        res = res || { totalCount: 0, items: [] };
        this.total = res.totalCount;
        this.list = res.items;
      });
    },
    handlePageChange(pageIndex) {
      this.filter.pageIndex = pageIndex;
      this.getData();
    },
    handleSizeChange(pageSize) {
      this.filter.pageSize = pageSize;
      this.getData();
    },
    handlePrePage(currentPageIndex) {
      this.filter.pageIndex = currentPageIndex - 1;
    },
    handleNextPage(currentPageIndex) {
      this.filter.pageIndex = currentPageIndex + 1;
    },
    handleEdit(id) {
      this.$router.push({
        name: "JournalEdit",
        query: {
          id: id,
        },
      });
    },
    handleDelete({ ids }) {
      if (!ids || ids.length == 0) return;
      for (let i = 0; i < ids.length; i++) {
        deleteJournal(ids[i]).then((res) => {
          if (!res) return;
          if (i == ids.length - 1) {
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
              position: "bottom-right",
            });
            this.getData();
          }
        });
      }
    },
    handleRefresh() {
      this.pageIndex = 1;
      this.pageSize = 10;
      this.getData();
    },
    hanldeDbClick(row) {
      this.handleEdit(row.id);
    },
  },
  computed: {
    dataSourceTypeName() {
      return function (type) {
        if (type == 0) return "手工";
        return "";
      };
    },
  },
};
</script>
